"""
生成器如何读取大文件
"""


def read_lines(f, new_line):
    buf = ""

    while True:
        while new_line in buf:
            pos = buf.index(new_line)
            yield buf[:pos]
            buf = buf[pos + len(new_line):]

        chunk = f.read(4096)
        if not chunk:
            # 边界条件（文件已读取完）
            yield buf
            break

        buf += chunk


def read_file():
    with open("c06_file.txt") as f:
        for line in read_lines(f, "{|}"):
            print(line)

    # 输出结果：
    """
    life is short
    you need python
    ai
    """


if __name__ == '__main__':
    read_file()
